package com.pos43.business.dao.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.pos43.business.dao.IProductDao;
import com.pos43.domain.Product;

public class ProductDao extends HibernateDaoSupport implements IProductDao{

	public Product get(Integer id){
		return getHibernateTemplate().get(Product.class, id);
	}

	public Integer save(Product product){
		return (Integer)getHibernateTemplate()
			.save(product);
	}
	
	public void update(Product product){
		getHibernateTemplate().update(product);
	}
	
	public void delete(Product product){
		getHibernateTemplate().delete(product);
	}
	
	public void delete(Integer id){
		getHibernateTemplate().delete(get(id));
	}
	
	public List<Product> findAll(){
		return (List<Product>)getHibernateTemplate()
				.find("from Product");
	}

	@Override
	public List<Product> findByParam(String key) {
		String hql = "from Product p where p.pname like '%" + key + "%' or p.productType.ptname like '%" + key + "%'";
		return this.getSession()
				.createQuery(hql)
				.list();
		}

}
